<template>
  <el-dialog title="请录入日期" :visible="visible" @close="close">
    <el-form ref="date-form" :model="form">
      <el-form-item prop="date" :rules="[{ required: true, message: '不能为空'}]">
        <el-date-picker v-model="form.date" type="date" placeholder="选择日期" style="width: 100%;" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { newShopDate } from '@/api/replacementApi';
export default {
  name: 'EntryDate',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    date: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      form: {
        date: ''
      }
    };
  },
  watch: {
    date(val) {
      this.form.date = this.moment(val).subtract(1, 'months').startOf('month').format('YYYY-MM-DD');
    }
  },
  methods: {
    // 提交弹窗内容
    submit() {
      this.$refs['date-form'].validate(valid => {
        if (valid) {
          newShopDate({ date: this.date, newShopDate: this.moment(this.form.date).format('YYYY-MM-DD') }).then(() => {
            this.$message.success('录入成功！');
            this.$emit('submit');
            this.$emit('close');
          });
        }
      });
    },
    // 完成关闭弹窗
    close() {
      this.$refs['date-form'].resetFields();
      this.$emit('close');
    }
  }
};
</script>
